package org.ldx.lotterysystem.controller;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.ldx.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import org.ldx.lotterysystem.common.exception.ControllerException;
import org.ldx.lotterysystem.common.pojo.CommonResult;
import org.ldx.lotterysystem.common.utils.JacksonUtil;
import org.ldx.lotterysystem.controller.param.CreateActivityParam;
import org.ldx.lotterysystem.controller.param.PageParam;
import org.ldx.lotterysystem.controller.result.CreateActivityResult;
import org.ldx.lotterysystem.controller.result.FindActivityListResult;
import org.ldx.lotterysystem.controller.result.FindPrizeListResult;
import org.ldx.lotterysystem.controller.result.GetActivityDetailResult;
import org.ldx.lotterysystem.service.ActivityService;
import org.ldx.lotterysystem.service.dto.ActivityDTO;
import org.ldx.lotterysystem.service.dto.ActivityDetailDTO;
import org.ldx.lotterysystem.service.dto.CreateActivityDto;
import org.ldx.lotterysystem.service.dto.PageListDTO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class ActivityController {
    @Resource
    ActivityService activityService;
    @RequestMapping("/activity/create")
    public CommonResult<CreateActivityResult> createActivity(@RequestBody @Validated CreateActivityParam param){
        log.info("createActivity param:{}", JacksonUtil.writeValueAsString(param));
        CreateActivityDto activityDto = activityService.createActivity(param);
        return CommonResult.success(convertToCreateActivityResult(activityDto));
    }

    /**
     * 翻页形式获取活动列表
     * @param param
     * @return
     */
    @RequestMapping("/activity/find-list")
    public CommonResult<FindActivityListResult> findActivityList(PageParam param){
        log.info("findActivityList param:{}",JacksonUtil.writeValueAsString(param));
        PageListDTO<ActivityDTO> activityDTOPageListDTO = activityService.findActivityList(param);
        return CommonResult.success(convertToFindActivityListResult(activityDTOPageListDTO));
    }

    /**
     * 查询活动详细信息
     * @param activityId
     * @return
     */
    @RequestMapping("/activity-detail/find")
    public CommonResult<GetActivityDetailResult> getActivityDetail(Long activityId) {
        log.info("getActivityDetail activityId:{}", activityId);
        ActivityDetailDTO detailDTO = activityService.getActivityDetail(activityId);
        return CommonResult.success(convertToGetActivityDetailResult(detailDTO));
    }

    private GetActivityDetailResult convertToGetActivityDetailResult(ActivityDetailDTO detailDTO) {
        if(null == detailDTO){
            throw new ControllerException(ControllerErrorCodeConstants.GET_ACTIVITY_DETAIL_ERROR);
        }
        GetActivityDetailResult result = new GetActivityDetailResult();
        result.setActivityId(detailDTO.getActivityId());
        result.setValid(detailDTO.valid());
        result.setActivityName(detailDTO.getActivityName());
        result.setDescription(detailDTO.getDesc());
        result.setUsers(detailDTO.getUserDTOList().stream()
                .map(userDTO -> {
                    GetActivityDetailResult.User user = new GetActivityDetailResult.User();
                    user.setUserId(userDTO.getUserId());
                    user.setUserName(userDTO.getUserName());
                    user.setValid(userDTO.valid());
                    return user;
                }).collect(Collectors.toList()));
        result.setPrizes(detailDTO.getPrizeDTOList().stream()
                        .sorted(Comparator.comparingInt(prizeDto->prizeDto.getTiers().getCode()))//按奖品等级排序
                .map(prizeDTO -> {
                    GetActivityDetailResult.Prize prize = new GetActivityDetailResult.Prize();
                    prize.setPrizeId(prizeDTO.getPrizeId());
                    prize.setName(prizeDTO.getName());
                    prize.setImageUrl(prizeDTO.getImageUrl());
                    prize.setPrice(prizeDTO.getPrice());
                    prize.setDescription(prizeDTO.getDescription());
                    prize.setPrizeTierName(prizeDTO.getTiers().getMessage());
                    prize.setPrizeAmount(prizeDTO.getPrizeAmount());
                    prize.setValid(prizeDTO.valid());
                    return prize;

                }).collect(Collectors.toList()));

        return result;
    }

    private FindActivityListResult convertToFindActivityListResult(PageListDTO<ActivityDTO> activityDTOPageListDTO) {
        if(null == activityDTOPageListDTO){
            throw new ControllerException(ControllerErrorCodeConstants.SELECT_ACTIVITY_ERROR);
        }
        FindActivityListResult result = new FindActivityListResult();
        result.setTotal(activityDTOPageListDTO.getTotal());
        result.setRecords(activityDTOPageListDTO.getRecords().stream().map(
                activityDTO -> {
                    FindActivityListResult.ActivityInfo activityInfo = new FindActivityListResult.ActivityInfo();
                    activityInfo.setActivityId(activityDTO.getActivityId());
                    activityInfo.setActivityName(activityDTO.getActivityName());
                    activityInfo.setDescription(activityDTO.getDescription());
                    //判断活动是不是RUNNING状态....
                    activityInfo.setValid(activityDTO.valid());
                    return activityInfo;
                }
        ).collect(Collectors.toList())
        );
        return result;

    }


    private CreateActivityResult convertToCreateActivityResult(CreateActivityDto activityDto) {
        if(null == activityDto){
            throw new ControllerException(ControllerErrorCodeConstants.CREATE_ACTIVITY_ERROR);
        }
        CreateActivityResult result = new CreateActivityResult();
        result.setActivityId(activityDto.getActivityId());
        return result;
    }



}
